package com.dq.domain.annex;

import com.dq.context.Constant;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.BaseEntity;

import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.user.base.SysUser;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ManyToOne;

/**
 * @创建人 gewenzhen
 * @创建时间 2019/9/15
 * @描述
 */
@Data
@Entity
@ApiModel("附件")
public class SysAnnex extends BaseEntity implements VO.ToVO {
    @ApiModelProperty(value = "附件编号", hidden = true)
    protected String annexNo = generyStrAndDatetimeAndRandomNum("SysAnnex_", Constant.ORDERS_SUFF_COUNT);
    @ManyToOne
    @ApiModelProperty(value = "用户", hidden = true)
    protected SysUser sysUser;

    @ManyToOne
    @ApiModelProperty(value = "组织部门", hidden = true)
    protected SysSection sysDept;

    @ApiModelProperty(value = "附件访问地址")
    protected String url;
    @ApiModelProperty(value = "附件远端存储名称")
    protected String urlName;
    @ApiModelProperty(value = "附件排序")
    protected Long sortNo;
    @ApiModelProperty(value = "附件名称")
    protected String name;
    @ApiModelProperty(value = "附件目标链接")
    protected String targetAddressUrl;
    /**
     * 0:身份正正面 1：身份正反面 2：用户头像 3：补充照片 4：商品列表展示
     * 5：商品首页轮播 6：商品详情图 7：商品小图 8：首页轮播图  9:首页分类列表 10:首页分类角标图片
     */
    @Enumerated(EnumType.ORDINAL)
    @ApiModelProperty(value = "附件类型")
    protected AnnexTypeEnum annexTypeEnum;

    @Data
    @ApiModel(value = "附件视图")
    public static class Vo implements VO {
        @ApiModelProperty(value = "附件id")
        private Long annexId;
        @ApiModelProperty(value = "附件编号")
        private String annexNo;
        @ApiModelProperty(value = "附件名称")
        private String name;
        @ApiModelProperty(value = "附件排序")
        private Long sortNo;
        @ApiModelProperty(value = "附件目标链接")
        private String targetAddressUrl;
        @ApiModelProperty(value = "显示地址")
        private String url;
        @ApiModelProperty(value = "用户id")
        private Long userId;
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.annexId = this.id;
        vo.annexNo = this.getAnnexNo();
        vo.name = this.getName();
        vo.sortNo = this.getSortNo();
        vo.targetAddressUrl = this.getTargetAddressUrl();
        vo.url = this.getShowUrl();
        vo.userId = this.getSysUser() == null ? null : this.getSysUser().getId();
        return vo;
    }

    public String getShowUrl() {
        return this.getUrl() + this.getUrlName();
    }
}
